package com.dianning.web.action.user;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.*;

import com.dianning.bean.Area;
import com.dianning.bean.Credit;
import com.dianning.bean.Info;
import com.dianning.bean.Money;
import com.dianning.bean.MoneyLog;
import com.dianning.bean.Reserve;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.OrderObj;
import com.dianning.comm.Util;
import com.dianning.service.AreaService;
import com.dianning.service.CreditService;
import com.dianning.service.InfoService;
import com.dianning.service.MoneyService;
import com.dianning.service.ReserveService;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.service.web.PageList;
import com.dianning.service.web.Pages;
import com.dianning.web.action.BaseAction;
import com.dianning.web.interceptor.UserSessionAware;
import com.dianning.web.servlet.UserSession;

public class InfoAction extends BaseAction implements UserSessionAware{

	private static final Log logger = LogFactory.getLog(InfoAction.class);
	
	private InfoService infoService;
	private CreditService creditService;
	private MoneyService moneyService;
	private ReserveService reserveService;
	private AreaService areaService;
	private UserInfoService userInfoService;

	public InfoService getInfoService() {
		return infoService;
	}

	public void setInfoService(InfoService infoService) {
		this.infoService = infoService;
	}

	public CreditService getCreditService() {
		return creditService;
	}

	public void setCreditService(CreditService creditService) {
		this.creditService = creditService;
	}

	public MoneyService getMoneyService() {
		return moneyService;
	}

	public void setMoneyService(MoneyService moneyService) {
		this.moneyService = moneyService;
	}

	public ReserveService getReserveService() {
		return reserveService;
	}

	public void setReserveService(ReserveService reserveService) {
		this.reserveService = reserveService;
	}

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}	
	
	private UserSession userSession;

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}
	
	private long id;
	private String ids;
	private int starthour;
	private int startminute;
	private int reserve_days;
	private String top_type;
	private List realTime_checkpage;
	private List reserve_checkpage;
	private int timelength;
	private String frm;
	

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}	

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getStarthour() {
		return starthour;
	}

	public void setStarthour(int starthour) {
		this.starthour = starthour;
	}

	public int getStartminute() {
		return startminute;
	}

	public void setStartminute(int startminute) {
		this.startminute = startminute;
	}

	public int getReserve_days() {
		return reserve_days;
	}

	public void setReserve_days(int reserve_days) {
		this.reserve_days = reserve_days;
	}

	public String getTop_type() {
		return top_type;
	}

	public void setTop_type(String top_type) {
		this.top_type = top_type;
	}

	public List getReserve_checkpage() {
		return reserve_checkpage;
	}

	public void setReserve_checkpage(List reserve_checkpage) {
		this.reserve_checkpage = reserve_checkpage;
	}

	public String getFrm() {
		return frm;
	}

	public void setFrm(String frm) {
		this.frm = frm;
	}
	private PageList pageList;
	private String message;
	private String free;
	private Money money;
	private int refreshMoney = Constant.REFRESH_MONEY;
	private int rateMoney = Constant.RATE_MONEY;
	private Info info;
	private List reserves;
	private List cityTopReserves;
	private List areaTopReserves;
	private List refreshReserves;
	private Area area;
	private long commendNum;
	private int refreshNum;
	private UserInfo userInfo;
	private Date today = new Date();
	private Date tomorrow = new Date(new Date().getTime() + 1000*60*60*24);

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getFree() {
		return free;
	}

	public void setFree(String free) {
		this.free = free;
	}

	public Money getMoney() {
		return money;
	}

	public void setMoney(Money money) {
		this.money = money;
	}

	public int getRefreshMoney() {
		return refreshMoney;
	}

	public void setRefreshMoney(int refreshMoney) {
		this.refreshMoney = refreshMoney;
	}

	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}

	public List getReserves() {
		return reserves;
	}

	public void setReserves(List reserves) {
		this.reserves = reserves;
	}

	public int getRateMoney() {
		return rateMoney;
	}

	public void setRateMoney(int rateMoney) {
		this.rateMoney = rateMoney;
	}

	public List getCityTopReserves() {
		return cityTopReserves;
	}

	public void setCityTopReserves(List cityTopReserves) {
		this.cityTopReserves = cityTopReserves;
	}

	public List getAreaTopReserves() {
		return areaTopReserves;
	}

	public void setAreaTopReserves(List areaTopReserves) {
		this.areaTopReserves = areaTopReserves;
	}

	public List getRefreshReserves() {
		return refreshReserves;
	}

	public void setRefreshReserves(List refreshReserves) {
		this.refreshReserves = refreshReserves;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public long getCommendNum() {
		return commendNum;
	}

	public void setCommendNum(long commendNum) {
		this.commendNum = commendNum;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public Date getTomorrow() {
		return tomorrow;
	}

	public void setTomorrow(Date tomorrow) {
		this.tomorrow = tomorrow;
	}
	
	public List getRealTime_checkpage() {
		return realTime_checkpage;
	}

	public void setRealTime_checkpage(List realTime_checkpage) {
		this.realTime_checkpage = realTime_checkpage;
	}

	public int getTimelength() {
		return timelength;
	}

	public void setTimelength(int timelength) {
		this.timelength = timelength;
	}

	public int getRefreshNum() {
		return refreshNum;
	}

	public void setRefreshNum(int refreshNum) {
		this.refreshNum = refreshNum;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}
	
	public String display(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/info.action?action=display");
		OrderObj orders = new OrderObj();
		orders.setOrderBy("addTime");
		orders.setAscOrDesc(1);
		OrderObj[] oo = {orders};
		this.setPageList(this.getInfoService().findInfos(userSession.getUid(), -1, -1, -1, -1, -1, 1, 1, -1, null, null, oo, pages));
		return "display";
	}
	
	public String verify(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/info.action?action=verify");
		OrderObj orders = new OrderObj();
		orders.setOrderBy("addTime");
		orders.setAscOrDesc(1);
		OrderObj[] oo = {orders};
		this.setPageList(this.getInfoService().findInfos(userSession.getUid(), -1, -1, -1, -1, -1, 0, -1, -1, null, null, oo, pages));
		return "verify";
	}
	
	public String close(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/info.action?action=close");
		OrderObj orders = new OrderObj();
		orders.setOrderBy("addTime");
		orders.setAscOrDesc(1);
		OrderObj[] oo = {orders};
		this.setPageList(this.getInfoService().findInfos(userSession.getUid(), -1, -1, -1, -1, -1, 1, 2, -1, null, null, oo, pages));
		return "close";
	}	
	
	public String del(){
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.USER_PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/user/info.action?action=del");
		OrderObj orders = new OrderObj();
		orders.setOrderBy("addTime");
		orders.setAscOrDesc(1);
		OrderObj[] oo = {orders};
		this.setPageList(this.getInfoService().findInfos(userSession.getUid(), -1, -1, -1, -1, -1, 1, 3, -1, null, null, oo, pages));
		return "del";
	}	
	
	public String dodisplay(){
		Info info = this.getInfoService().findInfoById(id);
		if(userSession.getUid() == info.getUid()){
			info.setStatus(1);
			this.getInfoService().updateInfo(info, null);
		}
		this.message = "{success:'true'}";
		return "message";
	}
	
	public String doclose(){
		Info info = this.getInfoService().findInfoById(id);
		if(userSession.getUid() == info.getUid()){
			info.setStatus(2);
			this.getInfoService().updateInfo(info, null);
		}
		this.message = "{success:'true'}";
		return "message";
	}
	
	public String dodel(){
		Info info = this.getInfoService().findInfoById(id);
		if(userSession.getUid() == info.getUid()){
			info.setStatus(3);
			this.getInfoService().updateInfo(info, null);
		}
		this.message = "{success:'true'}";
		return "message";
	}

	public String dodels(){
		List l = this.getInfoService().findInfos(-1, -1, -1, -1, -1, -1, -1, -1, -1, null, Util.string2List(ids), null);
		for(int i=0;i<l.size();i++){
			Info info = (Info) l.get(i);
			if(userSession.getUid() == info.getUid()){
				info.setStatus(3);
				this.getInfoService().updateInfo(info, null);
			}
		}
		this.message = "{success:'true'}";
		return "message";
	}
	
	public String refresh(){
		Credit c = this.getCreditService().findCreditByUidCodeTime(userSession.getUid(),"free_refresh");
		if(c==null){
			free="false";
			this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		}else{
			free="true";
		}
		c = this.getCreditService().findCreditByUidCode(userSession.getUid(),"free_refresh");
		if(c==null){
			free="true";
		}
		return "refresh";
	}
	
	public String allrefresh(){
		this.setRefreshNum(Util.string2List(ids).size());
		this.refreshMoney = Constant.REFRESH_MONEY*this.refreshNum;
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "allrefresh";
	}
	
	public String doallrefresh(){
		List l = Util.string2List(ids);
		for(int i=0;i<l.size();i++){
			long infoId = (Long) l.get(i);
			Info info = this.getInfoService().findInfoById(infoId);
			info.setUpTime(new Date());
			this.getInfoService().updateInfo(info, null);
		}
		this.refreshNum = Util.string2List(ids).size();

		Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
		
		if(m.getPoints()>=Constant.REFRESH_MONEY*this.refreshNum){
			//添加用户信用值
			UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
			ui.setCredit(ui.getCredit() + getMoneyCredit(m.getExpend(),Constant.REFRESH_MONEY*this.refreshNum));
			this.getUserInfoService().updateUserInfo(ui);
			m.setPoints(m.getPoints() - Constant.REFRESH_MONEY*this.refreshNum);
			m.setExpend(m.getExpend() + Constant.REFRESH_MONEY*this.refreshNum);
			MoneyLog ml = new MoneyLog();
			ml.setUid(userSession.getUid());
			ml.setUsername(userSession.getUsername());
			ml.setLogtype("out");
			ml.setPoints(-Constant.REFRESH_MONEY*this.refreshNum);
			ml.setExplain("刷新帖子 贴子编号为：" + this.ids);
			ml.setAddTime(new Date());
			this.getMoneyService().updateMoney(m, ml);
			this.setMoney(m);
			this.setMessage("true");
		}
		return "allrefresh";
	}
	
	public String dorefresh(){
		Credit c = this.getCreditService().findCreditByUidCode(userSession.getUid(), "free_refresh");
		if(c == null){
			c = new Credit();
			c.setUid(userSession.getUid());
			c.setCode("free_refresh");
			c.setCycletype(1);
			c.setPoints(0);
			c.setNum(1);
			c.setAddTime(new Date());
		}else{
			c.setAddTime(new Date());
		}
		this.getCreditService().createCredit(c);
		Info info = this.getInfoService().findInfoById(this.id);
		info.setUpTime(new Date());
		this.getInfoService().updateInfo(info, null);
		
		Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
		if(m.getPoints()>=Constant.REFRESH_MONEY){
			//添加用户信用值
			UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
			ui.setCredit(ui.getCredit() + getMoneyCredit(m.getExpend(),Constant.REFRESH_MONEY));
			this.getUserInfoService().updateUserInfo(ui);
			
			m.setPoints(m.getPoints() - Constant.REFRESH_MONEY);
			m.setExpend(m.getExpend() + Constant.REFRESH_MONEY);
			MoneyLog ml = new MoneyLog();
			ml.setUid(userSession.getUid());
			ml.setUsername(userSession.getUsername());
			ml.setLogtype("out");
			ml.setPoints(-Constant.REFRESH_MONEY);
			ml.setExplain("刷新帖子 贴子编号为：" + this.id);
			ml.setAddTime(new Date());
			this.getMoneyService().updateMoney(m, ml);
			this.free="false";
			this.setMoney(m);
			this.setMessage("true");
		}
		return "refresh";
	}

	public String freerefresh(){
		Credit c = this.getCreditService().findCreditByUidCode(userSession.getUid(), "free_refresh");
		if(c == null){
			c = new Credit();
			c.setUid(userSession.getUid());
			c.setCode("free_refresh");
			c.setCycletype(1);
			c.setPoints(0);
			c.setNum(1);
			c.setAddTime(new Date());
		}else{
			c.setAddTime(new Date());
		}
		this.getCreditService().createCredit(c);
		Info info = this.getInfoService().findInfoById(this.id);
		info.setUpTime(new Date());
		this.getInfoService().updateInfo(info, null);
		this.free="false";
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		this.setMessage("true");
		return "refresh";
	}
	
	
	public String top(){
		this.setInfo(this.getInfoService().findInfoById(this.id));
		if(this.getInfo() == null){
			return "infodisable";
		}else{
			if(this.getInfo().getValidated() == 2){
				return "infodisable";
			}
		}
		this.setCityTopReserves(this.getReserveService().findReserves(this.id, "city_top", 1, "id", 0));
		this.setAreaTopReserves(this.getReserveService().findReserves(this.id, "area_top", 1, "id", 0));
		if(this.getInfo().getAreaId() == 0){
			this.setArea(null);
		}else{
			this.setArea(this.getAreaService().findAreaById(this.getInfo().getAreaId()));
		}
		this.setCommendNum(this.getInfoService().getCommendInfoNum(this.getInfo().getCcid(), 1));
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "top";
	}
	
	public String dotop(){
		int checkpage = getCheckpage(this.realTime_checkpage);
		//选择了置顶
		if(checkpage>0){
			Info info = this.getInfoService().findInfoById(this.id);
			//普通置顶
			if(this.top_type.equalsIgnoreCase("normal")){
				if(checkpage==1){
					info.setIsAreaTop(1);
					info.setAreaTopTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+getNormalTime(this.timelength), Util.getMinute(), Util.getSecond()));
					this.getInfoService().updateInfo(info, null);
				}
				if(checkpage==20){
					info.setIsCityTop(1);
					info.setCityTopTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+getNormalTime(this.timelength), Util.getMinute(), Util.getSecond()));
					this.getInfoService().updateInfo(info, null);
				}
				if(checkpage==21){
					info.setIsAreaTop(1);
					info.setAreaTopTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+getNormalTime(this.timelength), Util.getMinute(), Util.getSecond()));
					info.setIsCityTop(1);
					info.setCityTopTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+getNormalTime(this.timelength), Util.getMinute(), Util.getSecond()));
					this.getInfoService().updateInfo(info, null);
				}
			}
			//高级置顶
			if(this.top_type.equalsIgnoreCase("advanced")){
				info.setIsCommend(1);
				info.setCommendTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+getAdvancedTime(this.timelength), Util.getMinute(), Util.getSecond()));
				this.getInfoService().updateInfo(info, null);
			}
			
		}
		int topMoney = checkpage * timelength;	
		Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
		if(m.getPoints()>=topMoney){
			//添加用户信用值
			UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
			ui.setCredit(ui.getCredit() + getMoneyCredit(m.getExpend(),topMoney));
			this.getUserInfoService().updateUserInfo(ui);
			
			m.setPoints(m.getPoints() - topMoney);
			m.setExpend(m.getExpend() + topMoney);
			MoneyLog ml = new MoneyLog();
			ml.setUid(userSession.getUid());
			ml.setUsername(userSession.getUsername());
			ml.setLogtype("out");
			ml.setPoints(-topMoney);
			ml.setExplain("置顶帖子 贴子编号为："+this.id);
			ml.setAddTime(new Date());
			this.getMoneyService().updateMoney(m, ml);	
			this.setMessage("true");
			
			this.setInfo(this.getInfoService().findInfoById(this.id));
			if(this.getInfo() == null){
				return "infodisable";
			}else{
				if(this.getInfo().getValidated() == 2){
					return "infodisable";
				}
			}
			this.setCityTopReserves(this.getReserveService().findReserves(this.id, "city_top", 1, "id", 0));
			this.setAreaTopReserves(this.getReserveService().findReserves(this.id, "area_top", 1, "id", 0));
			if(this.getInfo().getAreaId() == 0){
				this.setArea(null);
			}else{
				this.setArea(this.getAreaService().findAreaById(this.getInfo().getAreaId()));
			}
			this.setCommendNum(this.getInfoService().getCommendInfoNum(this.getInfo().getCcid(), 1));
			this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		}
		return "top";
	}
	
	public String autotop(){
		this.setInfo(this.getInfoService().findInfoById(this.id));
		if(this.getInfo() == null){
			return "infodisable";
		}else{
			if(this.getInfo().getValidated() == 2){
				return "infodisable";
			}
		}
		this.setReserves(this.getReserveService().findReserves(this.id, "city_top", 1, "addTime", 0));
		if(this.getReserves() == null){
			this.setReserves(this.getReserveService().findReserves(this.id, "area_top", 1, "addTime", 0));
		}
		if(this.getInfo().getAreaId() == 0){
			this.setArea(null);
		}else{
			this.setArea(this.getAreaService().findAreaById(this.getInfo().getAreaId()));
		}
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		return "autotop";
	}
	
	public String doautotop(){
		int checkpage = getCheckpage(this.reserve_checkpage);
		//选择了置顶
		if(checkpage>0){
			if(checkpage==1){
				for(int i=1;i<=reserve_days;i++){
					Reserve r = new Reserve();
					r.setUid(userSession.getUid());
					r.setInfoId(this.id);
					r.setType("area_top");
					r.setStartTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour, startminute, 0));
					r.setEndTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour+getAutoTopTime(timelength), startminute, 0));
					r.setStatus(1);
					r.setAddTime(new Date());
					this.getReserveService().saveReserve(r);
				}
			}
			if(checkpage==10){
				for(int i=1;i<=reserve_days;i++){
					Reserve r = new Reserve();
					r.setUid(userSession.getUid());
					r.setInfoId(this.id);
					r.setType("city_top");
					r.setStartTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour, startminute, 0));
					r.setEndTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour+getAutoTopTime(timelength), startminute, 0));
					r.setStatus(1);
					r.setAddTime(new Date());
					this.getReserveService().saveReserve(r);
				}
			}
			if(checkpage==11){
				for(int i=1;i<=reserve_days;i++){
					Reserve r = new Reserve();
					r.setUid(userSession.getUid());
					r.setInfoId(this.id);
					r.setType("city_top");
					r.setStartTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour, startminute, 0));
					r.setEndTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour+getAutoTopTime(timelength), startminute, 0));
					r.setStatus(1);
					r.setAddTime(new Date());
					this.getReserveService().saveReserve(r);
				}
				for(int i=1;i<=reserve_days;i++){
					Reserve r = new Reserve();
					r.setUid(userSession.getUid());
					r.setInfoId(this.id);
					r.setType("area_top");
					r.setStartTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour, startminute, 0));
					r.setEndTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay()+i, starthour+getAutoTopTime(timelength), startminute, 0));
					r.setStatus(1);
					r.setAddTime(new Date());
					this.getReserveService().saveReserve(r);
				}
			}
			
			int topMoney = checkpage * timelength* reserve_days;
			Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
			
			if(m.getPoints()>=topMoney){
				//添加用户信用值
				UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
				ui.setCredit(ui.getCredit() + getMoneyCredit(m.getExpend(),topMoney));
				this.getUserInfoService().updateUserInfo(ui);
				
				m.setPoints(m.getPoints() - topMoney);
				m.setExpend(m.getExpend() + topMoney);
				MoneyLog ml = new MoneyLog();
				ml.setUid(userSession.getUid());
				ml.setUsername(userSession.getUsername());
				ml.setLogtype("out");
				ml.setPoints(-topMoney);
				ml.setExplain("委托置顶帖子 贴子编号为："+id+" 置顶时间为："+getAutoTopTime(timelength)+"小时 委托持续天数为："+reserve_days+"天");
				ml.setAddTime(new Date());
				this.getMoneyService().updateMoney(m, ml);
			}
			this.setMessage("true");
			this.setInfo(this.getInfoService().findInfoById(this.id));
			if(this.getInfo() == null){
				return "infodisable";
			}else{
				if(this.getInfo().getValidated() == 2){
					return "infodisable";
				}
			}
			if(this.getInfo().getAreaId() == 0){
				this.setArea(null);
			}else{
				this.setArea(this.getAreaService().findAreaById(this.getInfo().getAreaId()));
			}
			this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
			this.setReserves(this.getReserveService().findReserves(this.id, "city_top", 1, "addTime", 0));
			if(this.getReserves() == null){
				this.setReserves(this.getReserveService().findReserves(this.id, "area_top", 1, "addTime", 0));
			}
		}
		return "autotop";
	}

	public String autorefresh(){
		this.setInfo(this.getInfoService().findInfoById(this.id));
		if(this.getInfo() == null){
			return "infodisable";
		}else{
			if(this.getInfo().getValidated() == 2){
				return "infodisable";
			}
		}
		this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
		this.setReserves(this.getReserveService().findReserves(this.id, "refresh", 1, "addTime", 0));
		return "autorefresh";
	}
	
	public String doautorefresh(){
		for(int i=1;i<=getAutoRefreshTime(timelength);i++){
			Reserve r = new Reserve();
			r.setUid(userSession.getUid());
			r.setInfoId(this.id);
			r.setType("refresh");
			r.setStartTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+4*i, Util.getMinute(), Util.getSecond()));
			r.setEndTime(Util.getTime(Util.getYear(), Util.getMonth(), Util.getDay(), Util.getHour()+4*i, Util.getMinute(), Util.getSecond()));
			r.setStatus(1);
			r.setAddTime(new Date());
			this.getReserveService().saveReserve(r);
		}
		Money m = this.getMoneyService().findMoneyByUid(userSession.getUid());
		
		if(m.getPoints()>=timelength){
			//添加用户信用值
			UserInfo ui = this.getUserInfoService().findUserInfoByUid(userSession.getUid());
			ui.setCredit(ui.getCredit() + getMoneyCredit(m.getExpend(),timelength));
			this.getUserInfoService().updateUserInfo(ui);
			
			m.setPoints(m.getPoints() - timelength);
			m.setExpend(m.getExpend() + timelength);
			MoneyLog ml = new MoneyLog();
			ml.setUid(userSession.getUid());
			ml.setUsername(userSession.getUsername());
			ml.setLogtype("out");
			ml.setPoints(-timelength);
			ml.setExplain("委托刷新帖子 贴子编号为："+id);
			ml.setAddTime(new Date());
			this.getMoneyService().updateMoney(m, ml);
			this.setMessage("true");
			this.setInfo(this.getInfoService().findInfoById(this.id));
			if(this.getInfo() == null){
				return "infodisable";
			}else{
				if(this.getInfo().getValidated() == 2){
					return "infodisable";
				}
			}
			this.setMoney(this.getMoneyService().findMoneyByUid(userSession.getUid()));
			this.setReserves(this.getReserveService().findReserves(this.id, "refresh", 1, "addTime", 0));
		}
		return "autorefresh";
	}
	
	
	
	private int getCheckpage(List checkpage){
		int sum = 0;		
		for(int i=0;i<checkpage.size();i++){
			sum+=Integer.parseInt((String) checkpage.get(i));
		}
		return sum;
	}
	
	private int getNormalTime(int timelength){
		if(timelength==1){
			return 6;
		}
		if(timelength==3){
			return 16;
		}
		if(timelength==12){
			return 24*3;
		}
		if(timelength==60){
			return 24*15;
		}
		if(timelength==120){
			return 24*30;
		}
		return 0;
	}
	
	private int getAdvancedTime(int timelength){
		if(timelength==2400){
			return 24*30;
		}
		if(timelength==4800){
			return 24*60;
		}
		if(timelength==7000){
			return 24*90;
		}
		if(timelength==12000){
			return 24*180;
		}
		if(timelength==20000){
			return 24*365;
		}
		return 0;
	}
	private int getAutoTopTime(int timelength){
		if(timelength==1){
			return 3;
		}
		if(timelength==2){
			return 6;
		}
		if(timelength==4){
			return 12;
		}
		if(timelength==5){
			return 15;
		}
		if(timelength==6){
			return 18;
		}
		return 0;
	}
	private int getAutoRefreshTime(int timelength){
		if(timelength==5){
			return 6;
		}
		if(timelength==10){
			return 6*3;
		}
		if(timelength==75){
			return 6*15;
		}
		if(timelength==150){
			return 6*30;
		}
		if(timelength==450){
			return 6*90;
		}
		return 0;
	}


	private int getMoneyCredit(int expend,int money){
		int number = expend%10;
		return (number+money)/10;
		
	}
	

}
